<?php

declare(strict_types=1);

namespace app\validate\notify;

use think\Validate;

class RecordValidate extends Validate
{
    protected $rule = [
        'id' => 'require|integer|gt:0',
        'recipient_ids' => 'checkRecipientIds',
    ];

    protected $message = [
        'id.require' => '通知ID不能为空',
        'id.integer' => '通知ID必须为整数',
        'id.gt' => '通知ID必须大于0',
    ];

    protected $scene = [
        'resend' => ['id', 'recipient_ids'],
    ];

    protected function checkRecipientIds($value)
    {
        if ($value === null || $value === '') {
            return true;
        }

        if (!is_array($value)) {
            return '接收者ID需为数组';
        }

        foreach ($value as $item) {
            if (!is_int($item) && !(is_string($item) && ctype_digit($item))) {
                return '接收者ID必须为整数';
            }
        }

        return true;
    }
}
